# editfontsdialog.py
# this is a dialog to allow the user to edit the fonts in a vocab.

import sys
from PyQt4 import QtGui, QtCore

class EditFontsDialog(QtGui.QDialog):
    def __init__(self, vocab, parent = None):
        QtGui.QDialog.__init__(self, parent)
        
        self.vocab = vocab
        self.fontEdits = {}
        self.newFonts = {}
        
        self.setWindowTitle('Edit Fonts')
        self.layout = QtGui.QVBoxLayout(self)
        self.gridLayout = QtGui.QGridLayout()
        self.buttonPanelA = QtGui.QHBoxLayout()
        self.buttonPanelB = QtGui.QHBoxLayout()
        
        for fontName in self.vocab.fonts:
            fontEdit = fontEditWidget(fontName, self.vocab.fonts[fontName], self.gridLayout, self.vocab.mainGui)
            self.fontEdits[fontName] = fontEdit
        
        self.newFontButton = QtGui.QPushButton('New Font', self)
        self.okButton = QtGui.QPushButton('OK', self)
        self.cancelButton = QtGui.QPushButton('Cancel', self)
        self.buttonPanelA.addWidget(self.newFontButton)
        self.buttonPanelA.addStretch()
        self.buttonPanelB.addWidget(self.okButton)
        self.buttonPanelB.addWidget(self.cancelButton)
        self.buttonPanelB.addStretch()
        self.layout.addLayout(self.gridLayout)
        self.layout.addLayout(self.buttonPanelA)
        self.layout.addLayout(self.buttonPanelB)
        
        self.connect(self.newFontButton, QtCore.SIGNAL('clicked()'), self.newFont)
        self.connect(self.okButton, QtCore.SIGNAL('clicked()'), self.ok)
        self.connect(self.cancelButton, QtCore.SIGNAL('clicked()'), self.cancel)
    
    def newFont(self):
        (ufontName, nameOK) = QtGui.QInputDialog.getText(self, 'New Font Name', 'Enter a name for the font')
        fontName = str(ufontName)
        if nameOK:
            if fontName in self.vocab.fonts.keys():
                QtGui.QMessageBox.information(self, 'Choose a unique name', "There is already a font calle %s. Choose a unique name." % (fontName))
            else:
                (newFont, fontOK) = QtGui.QFontDialog.getFont(QtGui.QFont(), None, 'Select new font')
                if fontOK:
                    fontEdit = fontEditWidget(fontName, newFont, self.gridLayout, self.vocab.mainGui)
                    self.fontEdits[fontName] = fontEdit
                    self.newFonts[fontName] = newFont
    
    def ok(self):
        self.vocab.fonts.update(self.newFonts)
        for key in self.fontEdits:
            self.vocab.fonts[key] = self.fontEdits[key].font
        self.vocab.refreshFonts()
        self.vocab.changed = True
        self.close()
    
    def cancel(self):
        self.close()

class fontEditWidget(QtCore.QObject):
    
    def __init__(self, fontName, font, grid, parent = None):
        QtCore.QObject.__init__(self, parent)
        
        self.fontName = fontName
        self.font = font
        self.fontShow = QtGui.QLabel(fontName)
        self.fontShow.setFont(self.font)
        # I don't think it's necessary for the user to be able to delete fonts
        #self.deleteButton = QtGui.QPushButton('Delete')
        self.editButton = QtGui.QPushButton('Edit')
        
        self.connect(self.editButton, QtCore.SIGNAL('clicked()'), self.selectFont)
        
        row = grid.rowCount()
        grid.addWidget(QtGui.QLabel(self.fontName), row, 0)
        grid.addWidget(self.fontShow, row, 1)
        grid.addWidget(self.editButton, row, 2)
    
    def selectFont(self):
        (newFont, ok) = QtGui.QFontDialog.getFont(self.font, None, 'Select new font')
        if ok:
            self.font = newFont
            self.fontShow.setFont(self.font)
